﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mattress.Engine.Matlab {
    internal class mxCellArray : mxArray {
        public mxCellArray(SafeArrayPtr pa) : base(pa, mxArrayType.Cell) { }

        public mxArray GetCell(int m, int n) {
            if (m < 0 || n < 0)
                throw new ArgumentOutOfRangeException("The m and n must be larger than zero.");

            if (m >= this.M || n >= this.N)
                throw new ArgumentOutOfRangeException("The m and n must be less than the size of the cell array.");

            SafeArrayPtr pt = matrix.mxGetCell(this.NativeObject, n * this.M + m);
            
            return mxArray.Create(pt);
        }

        public mxArray[,] ToArray() {
            int m = this.M, n = this.N;
            mxArray[,] array = new mxArray[m, n];

            for (int r = 0; r < m; r++) {
                for (int c = 0; c < n; c++) {
                    array[r, c] = mxArray.Create(matrix.mxGetCell(this.NativeObject, c * m + r));
                }
            }

            return array;
        }
    }
}
